package cn.hncu.booksManagementSystem.example.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.common.enums.UserTypeEnum;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.business.factory.UserEbiFactory;
import cn.hncu.booksManagementSystem.user.vo.UserModel;
import cn.hncu.booksManagementSystem.user.vo.UserQueryModel;

public class QueryPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tfdId;
	private JTextField tfdName;

	private JFrame mainFrame;
	//注入ebi
	private UserEbi ebi = UserEbiFactory.getInstanceOfUserEbi();
	/**
	 * Create the panel.
	 */
	public QueryPanel(JFrame mainFrame) {
		this.mainFrame = mainFrame;
		initContentPane();
	}

	public void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("查询用户");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(294, 25, 182, 37);
		add(label);
		
		JLabel label_1 = new JLabel("用户ID：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setForeground(Color.BLUE);
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_1.setBounds(83, 140, 109, 32);
		add(label_1);
		
		tfdId = new JTextField("");
		tfdId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdId.setColumns(10);
		tfdId.setBounds(202, 140, 204, 32);
		add(tfdId);
		
		JComboBox<String> cbbxType = new JComboBox<String>();
		cbbxType.addItem("请选择...");
		for (UserTypeEnum ute : UserTypeEnum.values()) {
			cbbxType.addItem(ute.getName());
		}
		cbbxType.setFont(new Font("微软雅黑", Font.BOLD, 24));
		cbbxType.setBounds(489, 140, 214, 32);
		add(cbbxType);
		
		tfdName = new JTextField();
		tfdName.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdName.setColumns(10);
		tfdName.setBounds(218, 258, 188, 32);
		add(tfdName);
		
		JLabel label_2 = new JLabel("用户名称：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setForeground(Color.BLUE);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_2.setBounds(83, 258, 136, 32);
		add(label_2);
		
		JButton btnQuery = new JButton("查询");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//1收集参数
				String id = tfdId.getText();
				String name = tfdName.getText();
				int type = 0;
				
				if (id !=null && !id.trim().matches("^\\d{0,5}$")) {
					JOptionPane.showMessageDialog(null, "用户id只能为数字,长度不超过5");
					return;
				}
				
				if (name!=null&&name.trim().length()>0) {
					boolean isLegal = name.trim().matches("^\\w{1,10}$");
					if (!isLegal) {
						JOptionPane.showMessageDialog(null, "用户名格式错误，只能由字母、数字和_组成，并且长度在1-10之间");
						return;
					}
				}
				
				int index = cbbxType.getSelectedIndex();
				if (index>0) {
					type = index;
				}
				//2组织参数
				UserQueryModel uqm = new UserQueryModel();
				uqm.setId(id);
				uqm.setName(name);
				uqm.setType(type);
				//3TODO：调用逻辑层
				Collection<UserModel> resCol = ebi.getUsersByCondition(uqm);
				
				//4导向结果界面
				mainFrame.setContentPane(new ListPanel(mainFrame,resCol));
				mainFrame.validate();
			}
		});
		btnQuery.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnQuery.setBounds(175, 354, 136, 45);
		add(btnQuery);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(476, 354, 136, 45);
		add(btnBack);
	}
}
